package Dao;

import Pojo.Student;
import Utils.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.util.List;

public class studentimply extends BaseDao implements studentDao{

    @Override
    public void add(Student student) {
        Connection conn = JDBCUtils.getConnection();
        String sql="insert into students(sname,pwd,age,address,state)values(?,?,?,?,?)";
        aduMthod(conn,sql,student.getSname(),student.getPwd(),student.getAge(),student.getAddress(),student.getState());
    }

    @Override
    public void deleteByid(String sno) {
        Connection conn = JDBCUtils.getConnection();
        String sql="delete from students where sno=?";
        aduMthod(conn,sql,sno);
    }

    @Override
    public void update(Student student) {
        Connection conn = JDBCUtils.getConnection();
        String sql="update students set sname=?,pwd=?,age=?,address=? where sno=?";
        aduMthod(conn,sql,student.getSname(),student.getPwd(),student.getAge(),student.getAddress(),student.getSno());
    }

    @Override
    public Student selectByno(Integer sno) {
        Connection conn = JDBCUtils.getConnection();
        String sql="select sno,sname,pwd,age,address from students where sno=?";
        Student student = queryOne(Student.class, conn, sql, sno);
        return student;
    }

    @Override
    public Student selectBysnameAndpwd(String sname,String pwd) {
        Connection conn = JDBCUtils.getConnection();
        String sql="select sno,sname,pwd,age,address from students where sname=? and pwd=?";
        Student student = queryOne(Student.class, conn, sql, sname, pwd);
        return student;
    }

    @Override
    public List<Student> selectForlist() {
        Connection conn = JDBCUtils.getConnection();
        String sql="select sno,sname,pwd,age,address from students ";
        List<Student> list = queryForlist(Student.class, conn, sql);
        return list;
    }

    public int modPwd(String pwd,String sno) {
        Connection conn = JDBCUtils.getConnection();
        String sql="update students set pwd=? where sno=?";
        return  aduMthod(conn,sql,sno,pwd);
    }

    @Override
    public Student queryUserByUsernameAndPassword(String username, String password) {
        Connection conn = JDBCUtils.getConnection();
        String sql="select sno,sname,pwd,age,address,state from students where sname=? and pwd=?";
        return  queryOne(Student.class,conn,sql,username,password);
    }

    @Override
    public Integer queryPageTotalCount() {
        Connection conn = JDBCUtils.getConnection();
        String sql="select count(*) from students";
        Number count = (Number) queryForSingleValue(conn, sql);
       return count.intValue();
    }

    @Override
    public List<Student> queryPageItem(int begin, int pageSize) {
        Connection conn = JDBCUtils.getConnection();
        String sql="select sno,sname,pwd,age,address from students  limit ?,?";
        return queryForlist(Student.class,conn,sql,begin,pageSize);
    }

}
